unit traitselector;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  Spin, ButtonPanel, database, fpjson, LCLTranslator;

const
  TRAIT_PAGE_RATES  = 0;
  TRAIT_PAGE_STATS  = 1;
  TRAIT_PAGE_ATTACK = 2;
  TRAIT_PAGE_SKILL  = 3;
  TRAIT_PAGE_EQUIP  = 4;
  TRAIT_PAGE_OTHER  = 5;

type

  { TTraitSelectorForm }

  TTraitSelectorForm = class(TForm)
    ActionTimesRadioButton: TRadioButton;
    ActionTimesFloatSpinEdit: TFloatSpinEdit;
    AttackStatePercentLabel1: TLabel;
    KnockoutEffectRadioButton: TRadioButton;
    KnockoutEffectComboBox: TComboBox;
    PartyAbilityComboBox: TComboBox;
    PartyAbilityRadioButton: TRadioButton;
    SpecialFlagRadioButton: TRadioButton;
    SpecialFlagComboBox: TComboBox;
    LockEquipComboBox: TComboBox;
    LockEquipRadioButton: TRadioButton;
    EquipWeaponComboBox: TComboBox;
    AddSkillTypeRadioButton: TRadioButton;
    EquipWeaponRadioButton: TRadioButton;
    SealEquipComboBox: TComboBox;
    SlotTypeComboBox: TComboBox;
    SealEquipRadioButton: TRadioButton;
    SlotTypeRadioButton: TRadioButton;
    SealSkillTypeComboBox: TComboBox;
    AddSkillComboBox: TComboBox;
    SealSkillComboBox: TComboBox;
    EquipArmourComboBox: TComboBox;
    SealSkillTypeRadioButton: TRadioButton;
    AddSkillTypeComboBox: TComboBox;
    AttackSpeedFloatSpinEdit: TFloatSpinEdit;
    AttacksPerTurnPlusFloatSpinEdit: TFloatSpinEdit;
    AttackSpeedRadioButton: TRadioButton;
    AttacksPerTurnPlusRadioButton: TRadioButton;
    AttackStateRadioButton: TRadioButton;
    DummyOtherRadioButton: TRadioButton;
    DummyEquipRadioButton: TRadioButton;
    DummySkillRadioButton: TRadioButton;
    DummyRatesRadioButton: TRadioButton;
    DummyAttackRadioButton: TRadioButton;
    DummyStatsRadioButton: TRadioButton;
    ExParamComboBox: TComboBox;
    AttackElementRadioButton: TRadioButton;
    AttackStateComboBox: TComboBox;
    AttackStateFloatSpinEdit: TFloatSpinEdit;
    AttackStatePlusSignLabel: TLabel;
    AttackStatePercentLabel: TLabel;
    ResultButtonPanel: TButtonPanel;
    AddSkillRadioButton: TRadioButton;
    SealSkillRadioButton: TRadioButton;
    EquipArmourRadioButton: TRadioButton;
    SpParamComboBox: TComboBox;
    ExParamFloatSpinEdit: TFloatSpinEdit;
    SpParamFloatSpinEdit: TFloatSpinEdit;
    ExParamPlusSignLabel: TLabel;
    SpParamMultiplySignLabel: TLabel;
    ExParamPercentLabel: TLabel;
    SpParamPercentLabel: TLabel;
    ParamRadioButton: TRadioButton;
    ParamComboBox: TComboBox;
    ParamFloatSpinEdit: TFloatSpinEdit;
    ParamMultiplySignLabel: TLabel;
    ParamPercentLabel: TLabel;
    ExParamRadioButton: TRadioButton;
    SpParamRadioButton: TRadioButton;
    StateRateRadioButton: TRadioButton;
    ElemRateElemComboBox: TComboBox;
    ElementRateRadioButton: TRadioButton;
    DebuffRateRadioButton: TRadioButton;
    DebuffRateParamComboBox: TComboBox;
    StateResistRadioButton: TRadioButton;
    StateRateStateComboBox: TComboBox;
    ElemRateFloatSpinEdit: TFloatSpinEdit;
    DebuffRateFloatSpinEdit: TFloatSpinEdit;
    StateRateFloatSpinEdit: TFloatSpinEdit;
    ElemRateMultiplySignLabel: TLabel;
    DebuffRateMultiplySignLabel: TLabel;
    StateRateMultiplySignLabel: TLabel;
    ElemRatePercentLabel: TLabel;
    DebuffRatePercentLabel: TLabel;
    StateRatePercentLabel: TLabel;
    RatesTabSheet: TTabSheet;
    StateResistStateComboBox: TComboBox;
    AttackElementComboBox: TComboBox;
    StatsTabSheet: TTabSheet;
    AttackTabSheet: TTabSheet;
    SkillTabSheet: TTabSheet;
    EquipTabSheet: TTabSheet;
    OtherTabSheet: TTabSheet;
    TraitSelectorPageControl: TPageControl;
    procedure ActionTimesRadioButtonChange(Sender: TObject);
    procedure AddSkillRadioButtonChange(Sender: TObject);
    procedure AddSkillTypeRadioButtonChange(Sender: TObject);
    procedure AttackElementRadioButtonChange(Sender: TObject);
    procedure AttackSpeedRadioButtonChange(Sender: TObject);
    procedure AttackStateRadioButtonChange(Sender: TObject);
    procedure AttacksPerTurnPlusRadioButtonChange(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure KnockoutEffectRadioButtonChange(Sender: TObject);
    procedure DebuffRateRadioButtonChange(Sender: TObject);
    procedure ElementRateRadioButtonChange(Sender: TObject);
    procedure EquipArmourRadioButtonChange(Sender: TObject);
    procedure EquipWeaponRadioButtonChange(Sender: TObject);
    procedure ExParamRadioButtonChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure LockEquipRadioButtonChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure ParamRadioButtonChange(Sender: TObject);
    procedure PartyAbilityRadioButtonChange(Sender: TObject);
    procedure SealEquipRadioButtonChange(Sender: TObject);
    procedure SealSkillRadioButtonChange(Sender: TObject);
    procedure SealSkillTypeRadioButtonChange(Sender: TObject);
    procedure SlotTypeRadioButtonChange(Sender: TObject);
    procedure SpecialFlagRadioButtonChange(Sender: TObject);
    procedure SpParamRadioButtonChange(Sender: TObject);
    procedure StateRateRadioButtonChange(Sender: TObject);
    procedure StateResistRadioButtonChange(Sender: TObject);
  private
    ResultWasAccepted: Boolean;
    Db: TDatabase;

    { Move dummy radio buttons out of view without hiding, because I haven't
      found another way to unselect all the radio buttons :(( }
    procedure RepositionDummyRatioButtons;
    { Makes radio buttons unset on all pages except the page with the index
      passed (pass -1 to uncheck all pages). }
    procedure UnsetOptionsOnOtherPages(PageToKeep: Integer);
    procedure UnsetOptionsOnUnselectedPages;
    function GetSelectedTraitCode: Integer;
    function GetDataId(TraitCode: Integer): Integer;
    function GetValue(TraitCode: Integer): Double;
    procedure PresetValues(TraitCode: Integer; DataId: Integer; Value: Double);
  public
    SelectedCode: Integer;
    SelectedDataId: Integer;
    SelectedValue: Double;
    function ShowTraitSelection(ADb: TDatabase; TraitCode: Integer = -1; DataId: Integer = 0; Value: Double = 0): Boolean;
  end;

var
  TraitSelectorForm: TTraitSelectorForm;

implementation

uses
  comboboxhelper;

{$R *.lfm}

{ TTraitSelectorForm }

procedure TTraitSelectorForm.FormCreate(Sender: TObject);
begin
  RepositionDummyRatioButtons;
  UnsetOptionsOnOtherPages(-1);
end;

procedure TTraitSelectorForm.FormHide(Sender: TObject);
begin
  UnsetOptionsOnOtherPages(-1);

  (* We clear the combobox values because they can be changed next time when
   the form is called (e.g. if user changes skill names, param names et al.),
   so we ensure they will be reloaded *)
  ElemRateElemComboBox.Items.Clear;
  DebuffRateParamComboBox.Items.Clear;
  StateRateStateComboBox.Items.Clear;
  StateResistStateComboBox.Items.Clear;

  ParamComboBox.Items.Clear;
  { Ex-Params and Sp-Params are not reloaded because users can't change them }
  //TODO: refresh params if language was changed?

  AttackElementComboBox.Items.Clear;
  AttackStateComboBox.Items.Clear;

  AddSkillTypeComboBox.Items.Clear;
  SealSkillTypeComboBox.Items.Clear;
  AddSkillComboBox.Items.Clear;
  SealSkillComboBox.Items.Clear;

  EquipWeaponComboBox.Items.Clear;
  EquipArmourComboBox.Items.Clear;
  LockEquipComboBox.Items.Clear;
  SealEquipComboBox.Items.Clear;
  { Slot type is not cleared because its options can't be changed}

  { Special flag, knockout effect and party ability are not cleared either }
end;

procedure TTraitSelectorForm.LockEquipRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillEquipTypeComboBox(LockEquipComboBox, Db);
  LockEquipComboBox.Enabled := LockEquipRadioButton.Checked;
end;

procedure TTraitSelectorForm.OKButtonClick(Sender: TObject);
begin
  SelectedCode := GetSelectedTraitCode;
  SelectedDataId := GetDataId(SelectedCode);
  SelectedValue := GetValue(SelectedCode);

  ResultWasAccepted := True;
  Close;
end;

procedure TTraitSelectorForm.ParamRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(ParamComboBox, Db);
  ParamComboBox.Enabled := ParamRadioButton.Checked;
  ParamFloatSpinEdit.Enabled := ParamRadioButton.Checked;
end;

procedure TTraitSelectorForm.PartyAbilityRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillPartyFlagComboBox(PartyAbilityComboBox);
  PartyAbilityComboBox.Enabled := PartyAbilityRadioButton.Checked;
end;

procedure TTraitSelectorForm.SealEquipRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillEquipTypeComboBox(SealEquipComboBox, Db);
  SealEquipComboBox.Enabled := SealEquipRadioButton.Checked;
end;

procedure TTraitSelectorForm.SealSkillRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSkillComboBox(SealSkillComboBox, Db);
  SealSkillComboBox.Enabled := SealSkillRadioButton.Checked;
end;

procedure TTraitSelectorForm.SealSkillTypeRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSkillTypeComboBox(SealSkillTypeComboBox, Db);
  SealSkillTypeComboBox.Enabled := SealSkillTypeRadioButton.Checked;
end;

procedure TTraitSelectorForm.SlotTypeRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  if SlotTypeComboBox.Items.Count = 0 then begin
     SlotTypeComboBox.Items.BeginUpdate;
       SlotTypeComboBox.Items.Add('Normal slots');
       SlotTypeComboBox.Items.Add('Two weapons');
     SlotTypeComboBox.Items.EndUpdate;
     SlotTypeComboBox.ItemIndex := 0;
  end;
  SlotTypeComboBox.Enabled := SlotTypeRadioButton.Checked;
end;


procedure TTraitSelectorForm.SpecialFlagRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSpecialFlagComboBox(SpecialFlagComboBox);
  SpecialFlagComboBox.Enabled := SpecialFlagRadioButton.Checked;
end;

procedure TTraitSelectorForm.SpParamRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSpParameterComboBox(SpParamComboBox);
  SpParamComboBox.Enabled := SpParamRadioButton.Checked;
  SpParamFloatSpinEdit.Enabled := SpParamRadioButton.Checked;
end;

procedure TTraitSelectorForm.StateRateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillStateComboBox(StateRateStateComboBox, Db);
  StateRateStateComboBox.Enabled := StateRateRadioButton.Checked;
  StateRateFloatSpinEdit.Enabled := StateRateRadioButton.Checked;
end;

procedure TTraitSelectorForm.StateResistRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillStateComboBox(StateResistStateComboBox, Db);
  StateResistStateComboBox.Enabled := StateResistRadioButton.Checked;
end;

procedure TTraitSelectorForm.RepositionDummyRatioButtons;
begin
  // I hate this. But I haven't found a better way to unselect all radio buttons
  DummyRatesRadioButton.Left := -100;
  DummyStatsRadioButton.Left := -100;
  DummyAttackRadioButton.Left := -100;
  DummySkillRadioButton.Left := -100;
  DummyEquipRadioButton.Left := -100;
  DummyOtherRadioButton.Left := -100;
end;

procedure TTraitSelectorForm.ElementRateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillElementComboBox(ElemRateElemComboBox, Db);
  ElemRateElemComboBox.Enabled := ElementRateRadioButton.Checked;
  ElemRateFloatSpinEdit.Enabled := ElementRateRadioButton.Checked;
end;

procedure TTraitSelectorForm.EquipArmourRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillArmourTypeComboBox(EquipArmourComboBox, Db);
  EquipArmourComboBox.Enabled := EquipArmourRadioButton.Checked;
end;

procedure TTraitSelectorForm.EquipWeaponRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillWeaponTypeComboBox(EquipWeaponComboBox, Db);
  EquipWeaponComboBox.Enabled := EquipWeaponRadioButton.Checked;
end;

procedure TTraitSelectorForm.ExParamRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillExParameterComboBox(ExParamComboBox);
  ExParamComboBox.Enabled := ExParamRadioButton.Checked;
  ExParamFloatSpinEdit.Enabled := ExParamRadioButton.Checked;
end;

procedure TTraitSelectorForm.DebuffRateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(DebuffRateParamComboBox, Db);
  DebuffRateFloatSpinEdit.Enabled := DebuffRateRadioButton.Checked;
  DebuffRateParamComboBox.Enabled := DebuffRateRadioButton.Checked;
end;

procedure TTraitSelectorForm.AttackElementRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillElementComboBox(AttackElementComboBox, Db);
  AttackElementComboBox.Enabled := AttackElementRadioButton.Checked;
end;

procedure TTraitSelectorForm.AddSkillTypeRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSkillTypeComboBox(AddSkillTypeComboBox, Db);
  AddSkillTypeComboBox.Enabled := AddSkillTypeRadioButton.Checked;
end;

procedure TTraitSelectorForm.AddSkillRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSkillComboBox(AddSkillComboBox, Db);
  AddSkillComboBox.Enabled := AddSkillRadioButton.Checked;
end;

procedure TTraitSelectorForm.ActionTimesRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  ActionTimesFloatSpinEdit.Enabled := ActionTimesRadioButton.Checked;
end;

procedure TTraitSelectorForm.AttackSpeedRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  AttackSpeedFloatSpinEdit.Enabled := AttackSpeedRadioButton.Checked;
end;

procedure TTraitSelectorForm.AttackStateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillStateComboBox(AttackStateComboBox, Db);
  AttackStateComboBox.Enabled := AttackStateRadioButton.Checked;
  AttackStateFloatSpinEdit.Enabled := AttackStateRadioButton.Checked;
end;

procedure TTraitSelectorForm.AttacksPerTurnPlusRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  AttacksPerTurnPlusFloatSpinEdit.Enabled := AttacksPerTurnPlusRadioButton.Checked;
end;

procedure TTraitSelectorForm.CancelButtonClick(Sender: TObject);
begin
  ResultWasAccepted := False;
end;

procedure TTraitSelectorForm.KnockoutEffectRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillKnockoutEffectComboBox(KnockoutEffectComboBox);
  KnockoutEffectComboBox.Enabled := KnockoutEffectRadioButton.Checked;
end;

procedure TTraitSelectorForm.UnsetOptionsOnOtherPages(PageToKeep: Integer);
begin
  if PageToKeep <> TRAIT_PAGE_RATES then
    DummyRatesRadioButton.Checked := True;
  if PageToKeep <> TRAIT_PAGE_STATS then
    DummyStatsRadioButton.Checked := True;
  if PageToKeep <> TRAIT_PAGE_ATTACK then
    DummyAttackRadioButton.Checked := True;
  if PageToKeep <> TRAIT_PAGE_SKILL then
    DummySkillRadioButton.Checked := True;
  if PageToKeep <> TRAIT_PAGE_EQUIP then
    DummyEquipRadioButton.Checked := True;
  if PageToKeep <> TRAIT_PAGE_OTHER then
    DummyOtherRadioButton.Checked := True;
end;

procedure TTraitSelectorForm.UnsetOptionsOnUnselectedPages;
begin
  UnsetOptionsOnOtherPages(TraitSelectorPageControl.ActivePageIndex);
end;

function TTraitSelectorForm.GetSelectedTraitCode: Integer;
begin
  if ElementRateRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ELEMENT_RATE
  else if DebuffRateRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_DEBUFF_RATE
  else if StateRateRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_STATE_RATE
  else if StateResistRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_STATE_RESIST

  else if ParamRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_PARAM
  else if ExParamRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_XPARAM
  else if SpParamRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_SPARAM

  else if AttackElementRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ATTACK_ELEMENT
  else if AttackStateRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ATTACK_STATE
  else if AttackSpeedRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ATTACK_SPEED
  else if AttacksPerTurnPlusRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ATTACK_TIMES

  else if AddSkillTypeRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_STYPE_ADD
  else if SealSkillTypeRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_STYPE_SEAL
  else if AddSkillRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_SKILL_ADD
  else if SealSkillRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_SKILL_SEAL

  else if EquipWeaponRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_EQUIP_WTYPE
  else if EquipArmourRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_EQUIP_ATYPE
  else if LockEquipRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_EQUIP_LOCK
  else if SealEquipRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_EQUIP_SEAL
  else if SlotTypeRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_SLOT_TYPE

  else if ActionTimesRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_ACTION_PLUS
  else if SpecialFlagRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_SPECIAL_FLAG
  else if KnockoutEffectRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_COLLAPSE_TYPE
  else if PartyAbilityRadioButton.Checked then
    GetSelectedTraitCode := TRAIT_PARTY_ABILITY
end;

function TTraitSelectorForm.GetDataId(TraitCode: Integer): Integer;
begin
  case TraitCode of
  TRAIT_ELEMENT_RATE:
    GetDataId := ElemRateElemComboBox.ItemIndex + 1;
  TRAIT_DEBUFF_RATE:
    GetDataId := DebuffRateParamComboBox.ItemIndex;
  TRAIT_STATE_RATE:
    GetDataId := StateRateStateComboBox.ItemIndex + 1;
  TRAIT_STATE_RESIST:
    GetDataId := StateResistStateComboBox.ItemIndex + 1;

  TRAIT_PARAM:
    GetDataId := ParamComboBox.ItemIndex;
  TRAIT_XPARAM:
    GetDataId := ExParamComboBox.ItemIndex;
  TRAIT_SPARAM:
    GetDataId := SpParamComboBox.ItemIndex;

  TRAIT_ATTACK_ELEMENT:
    GetDataId := AttackElementComboBox.ItemIndex + 1;
  TRAIT_ATTACK_STATE:
    GetDataId := AttackStateComboBox.ItemIndex + 1;

  TRAIT_STYPE_ADD:
    GetDataId := AddSkillTypeComboBox.ItemIndex + 1;
  TRAIT_STYPE_SEAL:
    GetDataId := SealSkillTypeComboBox.ItemIndex + 1;
  TRAIT_SKILL_ADD:
    GetDataId := AddSkillComboBox.ItemIndex + 1;
  TRAIT_SKILL_SEAL:
    GetDataId := SealSkillComboBox.ItemIndex + 1;

  TRAIT_EQUIP_WTYPE:
    GetDataId := EquipWeaponComboBox.ItemIndex + 1;
  TRAIT_EQUIP_ATYPE:
    GetDataId := EquipArmourComboBox.ItemIndex + 1;
  TRAIT_EQUIP_LOCK:
    GetDataId := LockEquipComboBox.ItemIndex + 1;
  TRAIT_EQUIP_SEAL:
    GetDataId := SealEquipComboBox.ItemIndex + 1;

  TRAIT_SPECIAL_FLAG:
    GetDataId := SpecialFlagComboBox.ItemIndex;
  TRAIT_COLLAPSE_TYPE:
    GetDataId := KnockoutEffectComboBox.ItemIndex;
  TRAIT_PARTY_ABILITY:
    GetDataId := PartyAbilityComboBox.ItemIndex;

  else
    GetDataId := 0
  end;
end;

function TTraitSelectorForm.GetValue(TraitCode: Integer): Double;
begin

  case TraitCode of
  TRAIT_ELEMENT_RATE:
    GetValue := ElemRateFloatSpinEdit.Value / 100.0;
  TRAIT_DEBUFF_RATE:
    GetValue := DebuffRateFloatSpinEdit.Value / 100.0;
  TRAIT_STATE_RATE:
    GetValue := StateRateFloatSpinEdit.Value / 100.0;

  TRAIT_PARAM:
    GetValue := ParamFloatSpinEdit.Value / 100.0;
  TRAIT_XPARAM:
    GetValue := ExParamFloatSpinEdit.Value / 100.0;
  TRAIT_SPARAM:
    GetValue := SpParamFloatSpinEdit.Value / 100.0;

  TRAIT_ATTACK_STATE:
    GetValue := AttackStateFloatSpinEdit.Value / 100.0;
  TRAIT_ATTACK_SPEED:
    GetValue := AttackSpeedFloatSpinEdit.Value;
  TRAIT_ATTACK_TIMES:
    GetValue := AttacksPerTurnPlusFloatSpinEdit.Value;

  TRAIT_SLOT_TYPE:
    GetValue := SlotTypeComboBox.ItemIndex;

  TRAIT_ACTION_PLUS:
    GetValue := ActionTimesFloatSpinEdit.Value / 100.0;

  else
     GetValue := 0
  end;
end;

function TTraitSelectorForm.ShowTraitSelection(ADb: TDatabase; TraitCode: Integer = -1; DataId: Integer = 0; Value: Double = 0): Boolean;
begin
  Db := ADb;
  ResultWasAccepted := False;

  PresetValues(TraitCode, DataId, Value);
  ShowModal;

  ShowTraitSelection := ResultWasAccepted;
end;

procedure TTraitSelectorForm.PresetValues(TraitCode: Integer; DataId: Integer; Value: Double);
begin
  case TraitCode of
  TRAIT_ELEMENT_RATE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_RATES;
      ElementRateRadioButton.Checked := True;
      ElemRateElemComboBox.ItemIndex := DataId - 1;
      ElemRateFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_DEBUFF_RATE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_RATES;
      DebuffRateRadioButton.Checked := True;
      DebuffRateParamComboBox.ItemIndex := DataId;
      DebuffRateFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_STATE_RATE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_RATES;
      StateRateRadioButton.Checked := True;
      StateRateStateComboBox.ItemIndex := DataId - 1;
      StateRateFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_STATE_RESIST: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_RATES;
      StateResistRadioButton.Checked := True;
      StateResistStateComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_PARAM: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_STATS;
      ParamRadioButton.Checked := True;
      ParamComboBox.ItemIndex := DataId;
      ParamFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_XPARAM: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_STATS;
      ExParamRadioButton.Checked := True;
      ExParamComboBox.ItemIndex := DataId;
      ExParamFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_SPARAM: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_STATS;
      SpParamRadioButton.Checked := True;
      SpParamComboBox.ItemIndex := DataId;
      SpParamFloatSpinEdit.Value := Value * 100.0;
    end;

  TRAIT_ATTACK_ELEMENT: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_ATTACK;
      AttackElementRadioButton.Checked := True;
      AttackElementComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_ATTACK_STATE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_ATTACK;
      AttackStateRadioButton.Checked := True;
      AttackStateFloatSpinEdit.Value := Value * 100.0;
    end;
  TRAIT_ATTACK_SPEED: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_ATTACK;
      AttackSpeedRadioButton.Checked := True;
      AttackStateFloatSpinEdit.Value := Value;
    end;
  TRAIT_ATTACK_TIMES: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_ATTACK;
      AttacksPerTurnPlusRadioButton.Checked := True;
      AttacksPerTurnPlusFloatSpinEdit.Value := Value;
    end;

  TRAIT_STYPE_ADD: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_SKILL;
      AddSkillTypeRadioButton.Checked := True;
      AddSkillTypeComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_STYPE_SEAL: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_SKILL;
      SealSkillTypeRadioButton.Checked := True;
      SealSkillTypeComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_SKILL_ADD: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_SKILL;
      AddSkillRadioButton.Checked := True;
      AddSkillComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_SKILL_SEAL: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_SKILL;
      SealSkillRadioButton.Checked := True;
      SealSkillComboBox.ItemIndex := DataId - 1;
    end;

  TRAIT_EQUIP_WTYPE: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_EQUIP;
      EquipWeaponRadioButton.Checked := True;
      EquipWeaponComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_EQUIP_ATYPE: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_EQUIP;
      EquipArmourRadioButton.Checked := True;
      EquipArmourComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_EQUIP_LOCK: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_EQUIP;
      LockEquipRadioButton.Checked := True;
      LockEquipComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_EQUIP_SEAL: begin
      TraitSelectorPageControl.ActivePageIndex:= TRAIT_PAGE_EQUIP;
      SealEquipRadioButton.Checked := True;
      SealEquipComboBox.ItemIndex := DataId - 1;
    end;
  TRAIT_SLOT_TYPE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_EQUIP;
      SlotTypeRadioButton.Checked := True;
      SlotTypeComboBox.ItemIndex := Round(Value) - 1;
    end;

  TRAIT_ACTION_PLUS: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_OTHER;
      ActionTimesRadioButton.Checked := True;
      ActionTimesFloatSpinEdit.Value := Value * 100;
    end;
  TRAIT_SPECIAL_FLAG: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_OTHER;
      SpecialFlagRadioButton.Checked := True;
      SpecialFlagComboBox.ItemIndex := DataId;
    end;
  TRAIT_COLLAPSE_TYPE: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_OTHER;
      KnockoutEffectRadioButton.Checked := True;
      KnockoutEffectComboBox.ItemIndex := DataId;
    end;
  TRAIT_PARTY_ABILITY: begin
      TraitSelectorPageControl.ActivePageIndex := TRAIT_PAGE_OTHER;
      PartyAbilityRadioButton.Checked := True;
      PartyAbilityComboBox.ItemIndex := DataId;
    end;
  else
    UnsetOptionsOnOtherPages(-1);
  end;
end;






end.

